home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 9
/
FM Towns Free Software Collection 9.iso
/
t_os
/
tool
/
tetujin
/
src
/
g_eff
/
ymedflt.c
< prev
next >
Wrap
Text File
|
1994-11-16
|
4KB
|
192 lines
/*
graphic effect lib.
Noise Filter
h. Toda 1994 5 9
*/
#include <stdio.h>
#include <stdlib.h>
#include <egb.h>
#include "g_eff.h"
#define NOERR 0 /* no error */
static int mx ;
static int aSen ;
static int mSen ;
static int cMax ;
static int aMax ;
static int x1 ;
static int y1 ;
static int x2 ;
static int y2 ;
static int (*read1)() ;
static int (*write)() ;
static int (*mask)() ;
g_yMedianFilter( BASICPARA *para )
{
unsigned char a[10][4] ;
int b[10][4] ;
int r0, g0, b0 ;
int i, x, y ;
int n ;
mx = para->mix ;
aSen = para->alphaSen ;
mSen = para->maskSen ;
cMax = para->colorMax ;
aMax = para->alphaMax ;
x1 = para->lupx ;
y1 = para->lupy ;
x2 = para->rdwx ;
y2 = para->rdwy ;
read1 = para->read1 ;
write = para->write ;
mask = para->mask ;
for( y = y1 ; y <= y2 ; y++ )
{
for( x=x1 ; x <= x2 ; x++ )
{
read1( x-1, y-1, a[0] ) ;
read1( x, y-1, a[1] ) ;
read1( x+1, y-1, a[2] ) ;
read1( x-1, y, a[3] ) ;
read1( x, y, a[4] ) ;
read1( x+1, y, a[5] ) ;
read1( x-1, y+1, a[6] ) ;
read1( x, y+1, a[7] ) ;
read1( x+1, y+1, a[8] ) ;
for( i=0 ; i<9 ; i++ )
{
rgbToYuv( a[i][0], a[i][1], a[i][2],
&(b[i][0]), &(b[i][1]), &(b[i][2]), cMax ) ;
}
n = ck9(
b[0][0], b[1][0], b[2][0],
b[3][0], b[4][0], b[5][0],
b[6][0], b[7][0], b[8][0]
) ;
yuvToRgb( b[n][0], b[4][1], b[4][2], &r0, &g0, &b0, cMax ) ;
a[9][0] = r0 ;
a[9][1] = g0 ;
a[9][2] = b0 ;
mixWrite( x, y, a[9], a[4] ) ;
}
}
return NOERR ;
}
static mixWrite( int x, int y, unsigned char *a, unsigned char *b )
{
unsigned char c[4] ;
int mix ;
if( mSen )
{
if( mask( x, y ) >= mSen )
return NOERR ;
}
mix = mx ;
if( aSen )
{
mix = mix * b[3] / aMax ;
}
c[0] = ( a[0] * mix + b[0] * ( 256 - mix ) + 0x80 ) >> 8 ;
c[1] = ( a[1] * mix + b[1] * ( 256 - mix ) + 0x80 ) >> 8 ;
c[2] = ( a[2] * mix + b[2] * ( 256 - mix ) + 0x80 ) >> 8 ;
c[3] = b[3] ;
if( mix )
{
write( x, y, c ) ;
}
return NOERR ;
}
static ck9( int a0, int a1, int a2, int a3,
int a4, int a5, int a6, int a7, int a8 )
{
int temp ;
int m[9] ;
int a[9] ;
int i, j ;
for( i=0 ; i<9 ; i++ )m[i] = i ;
a[0] = a0 ; a[1] = a1 ; a[2] = a2 ; a[3] = a3 ; a[4] = a4 ;
a[5] = a5 ; a[6] = a6 ; a[7] = a7 ; a[8] = a8 ;
for( j=1 ; j<6 ; j++ )
{
for( i=j ; i<9 ; i++ )
{
if( a[j-1] > a[i] )
{
temp = a[i] ;
a[i] = a[j-1] ;
a[j-1] =temp ;
temp = m[i] ;
m[i] = m[j-1] ;
m[j-1] =temp ;
}
}
}
return m[4] ;
}
static rgbToYuv( int r0, int g0, int b0, int *y0, int *u0, int *v0, int max )
{
int y, cb, cr ;
y = 19595*r0 + 38470*g0 + 7471*b0 ;
cb = -11049*r0 - 21699*g0 + 32748*b0 ;
cr = 32755*r0 -27427*g0 -5328*b0 ;
*y0 = ( ((y/max)*219) >> 16 ) + 16 ;
*u0 = ( ((cb/max)*224) >> 16 ) + 128 ;
*v0 = ( ((cr/max)*224) >> 16 ) + 128 ;
if( *y0 < 0 )*y0 = 0 ;
if( *y0 > 255 )*y0 = 255 ;
if( *u0 < 0 )*u0 = 0 ;
if( *u0 > 255 )*u0 = 255 ;
if( *v0 < 0 )*v0 = 0 ;
if( *v0 > 255 )*v0 = 255 ;
return NOERR ;
}
static yuvToRgb( int y0, int u0, int v0, int *r0, int *g0, int *b0, int max )
{
y0 = (( y0 - 16 ) * max )/219 ;
u0 = (( u0 -128 ) * max )/224 ;
v0 = (( v0 -128 ) * max )/224 ;
*r0 = ( 65536*y0 + 4*u0 + 91920*v0 ) >> 16 ;
*g0 = ( 65536*y0 - 22569*u0 - 46819*v0 ) >> 16 ;
*b0 = ( 65536*y0 + 116198*u0 - 9*v0 ) >> 16 ;
if( *r0 < 0 )*r0 = 0 ;
if( *r0 > max )*r0 = max ;
if( *g0 < 0 )*g0 = 0 ;
if( *g0 > max )*g0 = max ;
if( *b0 < 0 )*b0 = 0 ;
if( *b0 > max )*b0 = max ;
return NOERR ;
}